home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / utmp.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  2KB  |  71 lines

  1. /*
  2.  * BSD style utmp updating routine Version 1.0 (c) S.R.Usher 1991.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <utmp.h>
  10.  
  11. #define UTMP_FILE    "/etc/utmp"
  12.  
  13. void _write_utmp(line, name, host, time)
  14. const char *line, *name, *host;
  15. unsigned long time;
  16. {
  17.     register int returned_val;
  18.     int counter;
  19.     struct utmp entry;
  20.     int fd;
  21.  
  22.     bzero(&entry, sizeof(struct utmp));
  23.  
  24.     if ((fd = open(UTMP_FILE, O_RDWR)) == -1)
  25.     {
  26.         perror("_write_utmp");
  27.         return;
  28.     }
  29.  
  30.     for (counter = 0; ((returned_val = read(fd, &entry, (unsigned) sizeof(struct utmp))) != -1); counter++)
  31.     {
  32. #ifdef DEBUG
  33.         printf("Current line is '%s' (returned_val = %d)\n", entry.ut_line, returned_val);
  34. #endif
  35.         if (returned_val == 0)
  36.             break;
  37.         if (strncmp(line, entry.ut_line, 8) == 0)
  38.             break;
  39.     }
  40.  
  41.     if (lseek(fd, (long)(counter * sizeof(struct utmp)), 0) == -1)
  42.     {
  43.         perror("_write_utmp: lseek");
  44.         close(fd);
  45.         return;
  46.     }
  47.  
  48. /*
  49.  * Note, doing this in this order means that it doesn't matter about the Null
  50.  * bytes strncpy adds the the strings if they are greater than 8/16 bytes!
  51.  */
  52. #ifdef DEBUG
  53.     printf("counter = %d\nline = %s\nname = %s\nhost = %s\ntime = %lu\n",
  54.         counter, line, name, host, time);
  55. #endif
  56.     strncpy(entry.ut_line, line, 8);
  57.     strncpy(entry.ut_name, name, 8);
  58.     strncpy(entry.ut_host, host, 16);
  59.     entry.ut_time = time;
  60.  
  61.     if ((returned_val = write(fd, &entry, (unsigned) sizeof(struct utmp))) == -1)
  62.         perror("_write_utmp: write");
  63.     else
  64.         if (returned_val != sizeof(struct utmp))
  65.             fprintf(stderr, "_write_utmp: write: wrote too few bytes!\n");
  66. #ifdef DEBUG
  67.     printf("_write_utmp: wrote %d bytes\n", returned_val);
  68. #endif
  69.     close(fd);
  70. }
  71.